package com.fafa.yygh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fafa.yygh.cmn.client.DictFeignClient;
import com.fafa.yygh.enums.DictEnum;
import com.fafa.yygh.model.user.Patient;
import com.fafa.yygh.user.mapper.PatientMapper;
import com.fafa.yygh.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Sire
 * @version 1.0
 * @date 2022-02-20 21:03
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    private PatientMapper patientMapper;

    @Autowired
    private DictFeignClient dictFeignClient;

    /**
     * 获取就诊人列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<Patient> findAllPatient(Long userId) {
        List<Patient> patients = patientMapper
                .selectList(new QueryWrapper<Patient>()
                        .eq("user_id", userId));
        // 这里需要封装一下就诊人的信息（因为省市区都是数字，需要包装为对应的字典信息）
        patients.stream().forEach(item -> {
            this.packPatient(item);
        });
        return patients;
    }

    /**
     * 根据id获取就诊人信息
     *
     * @param id
     * @return
     */
    @Override
    public Patient getPatientId(Long id) {
        return this.packPatient(baseMapper.selectById(id));
    }


    /**
     * 封装数据
     *
     * @param patient
     */
    public Patient packPatient(Patient patient) {
        // 根据证件类型编码，获取证件类型具体指
        // 自己的证件类型
        String certificatesTypeString = dictFeignClient
                .getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType());
        // 联系人证件类型
        String contactsCertificatesTypeString = dictFeignClient
                .getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType());
        //省
        String provinceString = dictFeignClient.getName(patient.getProvinceCode());
        //市
        String cityString = dictFeignClient.getName(patient.getCityCode());
        //区
        String districtString = dictFeignClient.getName(patient.getDistrictCode());
        // 放到其他参数中
        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        // 拼接成一个完整的地址
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
        return patient;
    }
}
